/* Constructor */
BelCheckButton = {}

/* Initialization */
BelCheckButton.Initialize				= function( self, ... )
	
	/* Base stuff */
	BelButton.Initialize( self, ... )
	
	/* Default values */
	self.Checked = false
	self.OnCheckedChanged = nil
	
	/* Remove default mouse hooks */
	BelControls.ClearHooks( "OnCursorEntered", self )
	BelControls.ClearHooks( "OnCursorExited", self )
	BelControls.ClearHooks( "OnMousePressed", self )
	BelControls.ClearHooks( "OnMouseReleased", self )
	
	/* Add new mouse hooks */
	BelControls.AddHook( "OnCursorEntered", self, self.CursorEntered )
	BelControls.AddHook( "OnCursorExited", self, self.CursorExited )
	BelControls.AddHook( "OnMousePressed", self, self.MousePressed )
	BelControls.AddHook( "OnMouseReleased", self, self.MouseReleased )
end

/* Cursor enters the control */
BelCheckButton.CursorEntered			= function( self )
	if not self.Checked then
		BelControls.OnCursorEntered( self )
	end
end

/* Cursor exits the control */
BelCheckButton.CursorExited				= function( self )
	if not self.Checked then
		BelControls.OnCursorExited( self )
	end
end

/* Mouse is pressed on the control */
BelCheckButton.MousePressed				= function( self )
	self:SetChecked( not self.Checked )
end

/* Mouse is released */
BelCheckButton.MouseReleased			= function( self )
	self.CurrentControlState = CTRL_STATE_IN
end

/* Set the checked value from code */
BelCheckButton.SetChecked = function( self, value )
	
	/* Get previous value */
	local prevValue = self.Checked
	
	/* Update the value */
	self.Checked = util.tobool( value )
	
	/* If the value has been updated, raise an event */
	if prevValue != value then
		self:CheckedChanged( value )
	end
	
	/* Checked -> Unchecked */
	if prevValue and not value then
		if self.CurrentControlState != CTRL_STATE_OUT then
			BelControls.OnMouseReleased( self )
		else
			BelControls.OnCursorExited( self )
		end
		
	/* Unchecked -> checked */
	elseif not prevValue and value then
		self.CurrentControlState = CTRL_STATE_DOWN
		self.BackgroundFader.Animation.Keyframes[2]:SetContinuous()
		self.BackgroundFader:FadeIn()
	end
end

/* Hook for check value changed */
BelCheckButton.CheckedChanged = function( self, value )

	/* If something has been set to be done, do it */
	if self.OnCheckedChanged and type( self.OnCheckedChanged ) == "function" then
		self.OnCheckedChanged( self, value )
	end
end

/* Register the control */
vgui.Register( "BelCheckButton", BelCheckButton, "BelButton" )